package top.yj95.xiets.basic;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

/**
 * 文章《<a href="https://blog.csdn.net/xietansheng/article/details/74363221" target="_balnk">JavaSwing_2.2: JButton（按钮）</a>》中的示例代码
 * @author 杨军
 * @since 2023-12-22
 */
public class CustomizeJButtonDemo {
    
    public static void main(String[] args) {
        JFrame frame = new JFrame("测试窗口");
        frame.setSize(250, 200);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        
        JPanel panel = new JPanel();
        
        JButton btn = new JButton();
        
        // 设置按钮的默认图片
        btn.setIcon(new ImageIcon(CustomizeJButtonDemo.class.getResource("/assets/images/jbutton/button_normal.png")));
        
        // 设置按钮被点击时的图片
        btn.setPressedIcon(new ImageIcon(CustomizeJButtonDemo.class.getResource("/assets/images/jbutton/button_press.png")));
        
        // 不绘制边框
        btn.setBorderPainted(false);
        
        // 添加按钮点击事件监听器
        btn.addActionListener(e -> System.out.println("按钮被点击了"));
        
        panel.add(btn);
        
        frame.setContentPane(panel);
        frame.setVisible(true);
    }
    
}
